package interfaces.tables;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.event.mouse.IMouseListener;
import org.fenggui.event.mouse.MouseClickedEvent;
import org.fenggui.event.mouse.MouseDoubleClickedEvent;
import org.fenggui.util.Color;

import fileHandling.language.Lang;

import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.StaticContent;

public abstract class TableEntry extends StaticContent {
	
	protected List<IMouseListener> mouseListeners;
	protected HashMap<Lang, InterfaceLabel> labels;
	protected Table<?> table;
	
	public TableEntry(int width, int height, int x, Table<?> table) {
		super(width, height, x, 0);
		this.table = table;
		
		mouseListeners = new ArrayList<IMouseListener>();
		
		initLabels();
		
		layout();
	}
	
	public abstract Color getNormalBGColor();
	
	public abstract Color getSelectedBGColor();
	
	protected void initLabels() {
		labels = new HashMap<Lang, InterfaceLabel>();
		
		int labelX = 0;
		int labelY = 0;
		int labelHeight = height;
		for(int i = 0; i < table.getColumnNames().length; i++) {
			int nextWidth = (int)(width * table.getColumnWidths()[i]);
			
			InterfaceLabel label = new InterfaceLabel("", table.getLabelFont());
			label.setXY(labelX, labelY);
			label.setSize(nextWidth, labelHeight);
			addWidget(label);
			
			labels.put(table.getColumnNames()[i], label);
			
			labelX += nextWidth;
		}
	}
	
	public void setValueOf(Lang row, String text) {
		InterfaceLabel label = labels.get(row);
		label.setText(text);
		layout();
	}
	
	public void addMouseListener(IMouseListener l) { mouseListeners.add(l); }
	
	public void removeMouseListener(IMouseListener l) { mouseListeners.remove(l); }
	
	@Override
	public void mouseDoubleClicked(MouseDoubleClickedEvent mce) {
		super.mouseDoubleClicked(mce);
		
		for(IMouseListener l : mouseListeners) {
			l.mouseDoubleClicked(mce);
		}
	}
	
	@Override
	public void mouseClicked(MouseClickedEvent mce) {
		super.mouseClicked(mce);
		
		for(IMouseListener l : mouseListeners) {
			l.mouseClicked(mce);
		}
	}
	
	public void setSelected(boolean selected) {
		Color bgColor = (selected ? getSelectedBGColor() : getNormalBGColor());
		getAppearance().removeAll();
		
		getAppearance().add(new PlainBackground(bgColor));
	}
}